using System;
using System.Text;

namespace VatLid
{
	/// <summary>
	/// Summary description for StringUtil.
	/// </summary>
	public class StringUtil
	{
		public static String nvl(Object arg1, String arg2) 
		{
			if(arg1 == null || arg1.ToString().Equals(""))
			{
				return arg2;
			}
			else
			{
				return arg1.ToString();
			}
		}

		public static byte[] toByteBuffer(String textStr)
		{
			return Encoding.UTF8.GetBytes(textStr);
		}

		public static string toString(byte[] bytes)
		{
			Encoding instEncoding = new ASCIIEncoding();
			return instEncoding.GetString(bytes);
		}

		public static string toUTF8String(byte[] bytes)
		{
			Encoding instEncoding = new UTF8Encoding();
			return instEncoding.GetString(bytes);
		}

		public static int countSymbol(String source,String chrSymbol,int iOffset)
		{
			if(chrSymbol == null || chrSymbol.Length == 0)
				return 0;
			int iCount = 0;
			while((iOffset = source.IndexOf(chrSymbol,iOffset) + 1) > 0)
				iCount++;
			return iCount;
		}

		public static int indexOfLetter(String source,int iOffset)
		{
			char c;
			while(iOffset < source.Length)
			{
				c = (source.ToCharArray())[iOffset];
				if(c > ' ')
				{
					return iOffset;
				}
				else
				{
					iOffset++;
				}
			}
			return -1;
		}

		public static System.String replaceAll(System.String strSrc, System.String strFind, System.String strReplace)
		{
			if (strFind == null || strFind.Length == 0)
				return strSrc;
			int iLocation = 0;
			int iPrevLocation = 0;
			System.Text.StringBuilder strResult = new System.Text.StringBuilder();
			//UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1101_3"'
			while ((iLocation = strSrc.IndexOf(strFind, iLocation)) >= 0)
			{
				strResult.Append(strSrc.Substring(iPrevLocation, (iLocation) - (iPrevLocation)));
				strResult.Append(strReplace);
				iLocation += strFind.Length;
				iPrevLocation = iLocation;
			}
			strResult.Append(strSrc.Substring(iPrevLocation, (strSrc.Length) - (iPrevLocation)));
			return strResult.ToString();
		}

		public static System.String replaceAll(System.String strSrc, System.String strFind, System.String strReplace, int iMaxReplacement)
		{
			int iLocation = 0;
			if (strFind == null || strFind.Length == 0)
				return strSrc;
			int iPrevLocation = 0;
			int iCount = 0;
			System.Text.StringBuilder strResult = new System.Text.StringBuilder();
			//UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1101_3"'
			while ((iLocation = strSrc.IndexOf(strFind, iLocation)) >= 0 && iCount < iMaxReplacement)
			{
				strResult.Append(strSrc.Substring(iPrevLocation, (iLocation) - (iPrevLocation)));
				strResult.Append(strReplace);
				iCount++;
				iLocation += strFind.Length;
				iPrevLocation = iLocation;
			}
			strResult.Append(strSrc.Substring(iPrevLocation, (strSrc.Length) - (iPrevLocation)));
			return strResult.ToString();
		}

		public static System.String[] toStringArray(System.String strSource, System.String strSeparator)
		{
			System.Collections.ArrayList vtReturn = toStringVector(strSource, strSeparator);
			System.String[] strReturn = new System.String[vtReturn.Count];
			for (int iIndex = 0; iIndex < strReturn.Length; iIndex++)
				strReturn[iIndex] = ((System.String) vtReturn[iIndex]);
			return strReturn;
		}

		public static System.String[] toStringArray(System.String input)
		{
			return toStringArray(input, ",");
		}

		public static System.Collections.ArrayList toStringVector(System.String source, System.String separator)
		{
			System.Collections.ArrayList vtReturn = new System.Collections.ArrayList();
			int iIndex = 0;
			int iLastIndex = 0;
			while ((iIndex = source.IndexOf(separator, iLastIndex)) >= 0)
			{
				vtReturn.Add(source.Substring(iLastIndex, (iIndex) - (iLastIndex)).Trim());
				iLastIndex = iIndex + separator.Length;
			}
			if (iLastIndex <= source.Length)
				vtReturn.Add(source.Substring(iLastIndex, (source.Length) - (iLastIndex)).Trim());
			return vtReturn;
		}

        public static bool validatenum(string number)
        {
            bool ichar = true;
            for (int i = 0; i < number.Length; i++)
            {
                if (!char.IsNumber(number[i]))
                {
                    ichar = false;
                }
            }
            return ichar;
        }

	}
}
